﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace QuoteDAL
{
    public class HttpDAL
    {
        /// <summary>
        /// get方式的URL请求都可以用这个方法
        /// </summary>
        /// <param name="rUrl"></param>
        /// <param name="container"></param>
        /// <returns></returns>
        public static string ReadUrlContent(string rUrl, string Encode)
        {
            // 请求该页面
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rUrl);
            //request.CookieContainer = container;
            // request.Timeout = 4000;

            request.AllowAutoRedirect = false;
            request.Accept = "*/*";
            request.Headers.Add("Accept-Language", "zh-cn");
            //request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
            request.KeepAlive = true;
            request.Timeout = 300000;
            request.Method = "GET";


            string tempString = "";
            HttpWebResponse response = null;
            try
            {

                response = (HttpWebResponse)request.GetResponse();
                using (Stream streamReceive = response.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(streamReceive, System.Text.Encoding.GetEncoding(Encode)))
                    {
                        tempString = streamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
                return "";
            }
            return tempString;
        }
    }
}
